<?php

/*

steps for each neighborhood
Get the outer boundary with  st.astext(st.boundary(GIS_DATA))

get the number of interior rings (holes in the polygon) with st.numinteriorrings(gis_data)

get the interior ring point strings based on ring count st.astext(st.InteriorRingN(GIS_DATA,(ring id)))

example queries
select top 10 st.astext(st.boundary(GIS_DATA)) from king_co_blocks_labels
select st.astext(st.InteriorRingN(GIS_DATA,(st.numinteriorrings(gis_data)))) ,st.numinteriorrings(gis_data) from king_co_blocks_labels


There are pre-existing classes for generating KML files from geodata using XML libraries.   
We are just echoing out the info here instead - maybe it will be faster.

*/



$path = dirname(dirname(__FILE__));
require($path.'/lib/crt.php');

$crt = new CRT();
$crt->init();




//get the outer ring for single neighborhood
//requires gis data for the object
//returns a point string 
function getOuterRing($name1){
	global $crt;
	
	$sql = "select st.astext(st.boundary(gis_data)) from ".BLOCKS_TBL". where name1 = ".$crt->gisdb->qstr($name1);

	$row = $crt->gisdb->GetCol($sql);
	
	if($row){
	
		$coords = $row[0];
		
		$coords = str_replace("(","",$coords);
		$coords = str_replace(")","",$coords);
		
		$coords = substr($coords,15,strlen($coords)-15);
		
		$coords = explode(",",$coords);
		
		foreach($coords as $latlon){

			$pointstring .= str_replace(" ",",",$latlon)."\n";
			
		}
		
		//echo($pointstring);

	}
	//$pointstring = 'x';
	return $pointstring;

}

//get coordinate string for a single interior ring within a neighorhood
//requires gis data for the object and a count of interior rings in that object
//returns a point string formatted for KML
function getInteriorRing($name1,$numRings){
	global $crt;
	
	// get the coords for each interior ring
	for($i = 1; $i <= $numRings; $i++){

		$pointstring .= 
		"<LinearRing>
		  <coordinates>";
	
		$sql = "select st.astext(st.InteriorRingN(gis_data,".$i.")) FROM ".BLOCKS_TBL." WHERE name1 = ".$crt->gisdb->qstr($name1);
		
		
		
		$row = $crt->gisdb->GetCol($sql);
		
		
		
		if($row){
	
			$coords = $row[0];
			
			
			
			$coords = str_replace("(","",$coords);
			$coords = str_replace(")","",$coords);
			
			$coords = substr($coords,11,strlen($coords)-11);
			
			$coords = explode(",",$coords);

			foreach($coords as $latlon){

				$pointstring .= str_replace(" ",",",$latlon)."\n";
				
			}
			
			$pointstring .=  "</coordinates>         <!-- lon,lat[,alt] -->
			</LinearRing>";
			
			//echo($pointstring);
		}
	}	
	return $pointstring;
}
	








//main function to generate the kml output file
// requires an array of object ids

// needs to be modifed for output all vs output singel hood
// single hood creates this file content for each hood indvidually
// output all creates one file with multiply polygons. 

function generateKMLOutput($objectList){
	global $crt;

	//create the header for the file

	$kmlcontent = '<?xml version="1.0" encoding="UTF-8"?>
					<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
					<Document>
					<name><![CDATA[crt]]></name>
					<open>1</open>
					<Snippet maxLines="0"><![CDATA[]]></Snippet>
					<description><![CDATA[Exported on 11/13/2009]]></description>
					<Style id="FEATURES">
					<LineStyle>
					  <color>806E6E6E</color>
					  <width>0.5</width>
					</LineStyle>
					<PolyStyle>
					  <outline>1</outline>
					  <fill>1</fill>
					  <color>80F7C8EA</color>
					</PolyStyle>
					</Style>
					<Folder>
					<name>Features</name>
					<open>0</open>';

	//loop through the object list

	foreach ($objectList as $neighborhood){

	$sql = 'SELECT  Name1, st.numinteriorrings(gis_data) AS num_rings FROM ".BLOCKS_TBL." WHERE name1 = '.$crt->gisdb->qstr($neighborhood);
	
	$row = $crt->gisdb->GetRow($sql);
	//print_r($row);
	
		if($row){

			$kmlcontent .= 
				"\r\n <Polygon id='{$row[Name1]}'>
				  <!-- specific to Polygon -->
				  <extrude>0</extrude>                       <!-- boolean -->
				  <tessellate>0</tessellate>                 <!-- boolean -->
				  <altitudeMode>clampToGround</altitudeMode> 
						<!-- kml:altitudeModeEnum: clampToGround, relativeToGround, or absolute -->
						<!-- or, substitute gx:altitudeMode: clampToSeaFloor, relativeToSeaFloor -->
				  <outerBoundaryIs>
					<LinearRing>
					  <coordinates>".getOuterRing($row[Name1])."</coordinates>         <!-- lon,lat[,alt] -->     //// get the outer ring coords
					</LinearRing>
				  </outerBoundaryIs>";
				  
				  
				  if($row[num_rings]){
				  
				  $kmlcontent .=
				  "<innerBoundaryIs>".getInteriorRing($row[Name1],$row[num_rings])."</innerBoundaryIs>";  // pass the gis_data and the ring count
				  
				  }
				  
				$kmlcontent .="</Polygon></Folder>";
		}
	}


	$kmlcontent .= "</Document></kml>";

// return the content to another process or output a file directly from here


echo($kmlcontent);

}




?>
